package com.situ.mall.controller.front;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Result;
import com.situ.mall.entity.User;
import com.situ.mall.service.IUserService;
import com.situ.mall.service.impl.UserServiceImpl;

@Controller
@RequestMapping("/user")
public class FrontUserController {
	
	@Autowired
	private IUserService userService;

//前台
	
	//1. 转发到   login.jsp（登陆页面） 
	@RequestMapping("/getLoginJsp.shtml")
	public String getLoginJsp() {
		System.out.println("FrontUserController.getLoginJsp()");
		
		return "login";
	}
	
	//2.login.jsp 处理登陆验证  （ 用户名密码是否正确）
	@RequestMapping("/login.shtml")
	@ResponseBody
	public Result login(User formUser ,HttpSession httpSession) {
		System.out.println("FrontUserController.login()");
		System.out.println(formUser);
		
		User user  = userService.login(formUser);
		
		if (user == null) {
			return Result.error("登陆失败");
		} else {
			httpSession.setAttribute("user", user);
			return Result.success("登陆成功");
		}
		
	}
	
	//3.退出登录，销毁session
		@RequestMapping("/loginout.shtml")
		@ResponseBody
		public Result loginout(HttpServletRequest req) {
			System.out.println("LoginController.loginout()");
			
			//从作用域拿session
			HttpSession session = req.getSession();
			
			//销毁
			session.invalidate();
			
			//销毁后 提示成功
			return Result.success("退出成功, 即将跳转到首页……");
		}
	
}
